<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\Handlers\WeconHandler;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class DedustingAlarm extends Model
{
    protected $table = 'xfy_dedusting_alarms';

    const LEVEL_1 = 1;
    const LEVEL_2 = 2;
    const LEVEL_3 = 3;

    const ACTIVE_YES = 1;
    const ACTIVE_NO = 0;

    const LEVEL_TEXT = ['1' => '一级报警', '2' => '二级报警', '3' => '严重报警'];
    const ACTIVE_TEXT = ['0' => '禁用', '1' => '启用'];

    const CONDITION = ['>', '<', '>=', '<=', '='];
    const CONDITION_TEXT = ['>' => '大于', '<' => '小于', '>=' => '大于等于', '<=' => '小于等于', '=' => '等于'];

    const SETTING_CACHE_KEY = 'dedusting_alarm_setting_';

    public function device()
    {
        return $this->belongsTo(DedustingDevice::class);
    }

    public static function setCache($data, $cond)
    {
        $key = self::SETTING_CACHE_KEY.$data['device_id'].'_'.$data['point'];
        Redis::hSet($key, $cond, json_encode($data));
    }

    public static function getCache($deviceId, $point)
    {
        $key = self::SETTING_CACHE_KEY.$deviceId.'_'.$point;
        $settings = Redis::hGetAll($key);

        return $settings;
    }

    public static function delCache($deviceId, $point, $cond)
    {
        $key = self::SETTING_CACHE_KEY.$deviceId.'_'.$point;

        Redis::hDel($key, $cond);
    }    
}